package org.zfm.user.controller;

import org.zfm.user.domain.dto.RegisterDto;
import org.zfm.user.domain.dto.SmsCodeDto;
import org.zfm.user.domain.dto.ToBindDto;
import org.zfm.user.service.IUserService;
import org.zfm.user.domain.User;
import org.zfm.user.query.UserQuery;
import org.zfm.util.data.AjaxResult;
import org.zfm.util.data.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    public IUserService userService;


    /**
     * 保存和修改公用的
     * @param user  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody User user){
        try {
            if( user.getId()!=null)
                userService.update(user);
            else
                userService.insert(user);
            return AjaxResult.getBean();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            userService.delete(id);
            return AjaxResult.getBean();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.getBean().setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id) {
        try {
            User user = userService.selectById(id);
            return AjaxResult.getBean().setResult(user);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){
        try {
            List< User> list = userService.selectAll();
            return AjaxResult.getBean().setResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageData 分页对象
    */
    @PostMapping
    public AjaxResult json(@RequestBody UserQuery query)
    {
        try {
            PageData<User> pageList = userService.queryPageData(query);
            return AjaxResult.getBean().setResult(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }

    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterDto dto){
        try {
            userService.register(dto);
            return AjaxResult.getBean().setMessage("注册成功！！");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @PostMapping("/login")
    public AjaxResult wechatScanLogin(@RequestBody Map<String,String> map){
        try {
            return userService.wechatScanLogin(map);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setSuccess(false).setMessage(e.getMessage());
        }
    }

    /**
     *
     * @param toBindDto 封装了获取微信access_token和appid的 bindToken 以及电话和验证码
     * @return
     */
    @PostMapping("/bind")
    public AjaxResult bind(@RequestBody ToBindDto toBindDto){
        try {
            return userService.bind(toBindDto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @PostMapping("/reSetPassword")
    public AjaxResult reSetPassword(@RequestBody Map<String,Object> reSetPasswordParam){
        try {
            return userService.reSetPassword(reSetPasswordParam);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setSuccess(false).setMessage(e.getMessage());
        }
    }
    @PostMapping("/smsCodeLogin")
    public AjaxResult smsLoginCode(@RequestBody Map<String,Object> smsCodeLoginParams){
        try {

            return userService.smsLoginCode(smsCodeLoginParams);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setSuccess(false).setMessage(e.getMessage());
        }
    }
}
